package com.itrash.android.test;

import java.util.concurrent.Callable;

import com.itrash.android.R;
import com.itrash.android.activity.household.HouseholdMenuActivity;
import com.itrash.android.activity.user.AddDefaultHouseholdActivity;
import com.itrash.android.activity.user.UserLoginActivity;
import com.itrash.android.activity.user.UserRegistrationActivity;
import com.jayway.android.robotium.solo.Solo;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class ItrashActivityTestCase<T extends Activity> extends ActivityInstrumentationTestCase2<T> {
	protected Solo solo;
	
	public ItrashActivityTestCase(Class<T> activityClass) {
		super(activityClass);
	}
	
	@Override
	protected void setUp() throws Exception {
	    super.setUp();

	    setActivityInitialTouchMode(false);
	    
	    solo = new Solo(getInstrumentation(), getActivity());
	}
	
	
	@Override
    public void tearDown() throws Exception {
	    super.tearDown();
	    
		solo.finishOpenedActivities();
	}
	
	//
	// Activity transition verification functors
	//
	
	protected <U extends Activity> Callable<Boolean> activityStarted(final Class<U> clazz) {
        return new Callable<Boolean>() {
                public Boolean call() throws Exception {
                	return solo.getCurrentActivity().getClass() == clazz;                	
                }
        };
	}	

	
	protected Callable<Boolean> userRegistrationActivityStarted() {
        return new Callable<Boolean>() {
                public Boolean call() throws Exception {
                	return solo.getCurrentActivity() instanceof UserRegistrationActivity;
                }
        };
	}	
	
	protected Callable<Boolean> userLoginActivityStarted() {
        return new Callable<Boolean>() {
                public Boolean call() throws Exception {
                	return solo.getCurrentActivity() instanceof UserLoginActivity;
                }
        };
	}
	
	protected Callable<Boolean> addDefaultHouseholdActivityStarted() {
        return new Callable<Boolean>() {
                public Boolean call() throws Exception {
                	return solo.getCurrentActivity() instanceof AddDefaultHouseholdActivity;
                }
        };
	}
	
	protected Callable<Boolean> householdMenuActivityStarted() {
        return new Callable<Boolean>() {
                public Boolean call() throws Exception {
                	return solo.getCurrentActivity() instanceof HouseholdMenuActivity;
                }
        };
	}			
	
	//
	// UI verification helpers
	//
		
	protected void verifyHouseholdMenuActivityUI(String userName) {
		assertTrue(solo.getCurrentActivity() instanceof HouseholdMenuActivity);
		HouseholdMenuActivity householdMenuActivity = (HouseholdMenuActivity) solo.getCurrentActivity();
		
		assertTrue(householdMenuActivity.getProgressBarVisibility() == View.GONE);
		
		TextView userContextView = (TextView) householdMenuActivity.findViewById(R.id.user_context);
		assertTrue(userContextView.getText().toString().startsWith(userName, 0) &&
				   userContextView.getVisibility() == View.VISIBLE);
				
		ImageButton logoutButton = (ImageButton) householdMenuActivity.findViewById(R.id.logout_button);
		assertTrue(logoutButton.getVisibility() == View.VISIBLE);		
		
		ImageButton householdButton = (ImageButton) householdMenuActivity.findViewById(R.id.household_button);
		ImageButton barcodeScannerButton = (ImageButton) householdMenuActivity.findViewById(R.id.barcode_scanner_button);
		ImageButton productSearchButton = (ImageButton) householdMenuActivity.findViewById(R.id.product_search_button);
		ImageButton appSettingsButton = (ImageButton) householdMenuActivity.findViewById(R.id.app_settings_button);				
		assertTrue(householdButton.isEnabled() == true &&
				   barcodeScannerButton.isEnabled() == true &&
				   productSearchButton.isEnabled() == true &&
				   appSettingsButton.isEnabled() == true);
	}	
}
